#include <fstream>
#include "gc.h"
#include "itkImageFileReader.h"

std::string makePath (std::string pathTemplate, int sliceNo0, int sliceNo1)
{
  char path[200];
  if (sliceNo1 == -1) {
    sprintf(path, pathTemplate.c_str(), sliceNo0);
  }
  else {
    sprintf(path, pathTemplate.c_str(), sliceNo0, sliceNo1);
  }
  std::string ret = path;
  return ret;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
LabelImage::Pointer readLabelImage (const char* path)
{
  typedef itk::ImageFileReader<LabelImage> Reader;
  Reader::Pointer reader = Reader::New();
  reader->SetFileName(path);
  reader->Update();
  return reader->GetOutput();
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
LabelPixel getLabel (LabelImage::Pointer labelImage, 
		     int x, int y)
{
  LabelImage::IndexType index;
  index[0] = x;
  index[1] = y;
  return labelImage->GetPixel(index);
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void writeArrayBinary (int* array, int length, const char* filePath)
{
  ofstream fs(filePath, ofstream::binary);
  fs.write((char*)array, length * sizeof(int));
  fs.close();
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void readArrayBinary (int* array, int length, const char* filePath)
{
  ifstream fs(filePath, ifstream::binary);
  fs.read((char*)array, length * sizeof(int));
  fs.close();
}
